﻿using ProyectoSalvadorDelMundo.Helpers;
using System;
using System.Configuration;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProyectoSalvadorDelMundo.CatalogosGrupales.Asambleas
{
    public partial class Asambleas : PaginaBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                // Navegacion
                linkNav.NavigateUrl = ConfigurationManager.AppSettings.GetValues("linkNav-NavigateUrl")[0];
                linkNav.Text = ConfigurationManager.AppSettings.GetValues("linkNav-Text")[0];

                lblNavActiva.Text = ConfigurationManager.AppSettings.GetValues("lblNavActiva-Text")[0];
                lblNavActiva.CssClass = ConfigurationManager.AppSettings.GetValues("lblNavActiva-CssClass")[0];

                // Titulo
                lblTitulo.Text = ConfigurationManager.AppSettings.GetValues("lblTitulo-Text")[0];
                lblTitulo.CssClass = ConfigurationManager.AppSettings.GetValues("lblTitulo-CssClass")[0];

                // Agregar
                btnAgregar.Text = ConfigurationManager.AppSettings.GetValues("btnAgregar-Text")[0];
                btnAgregar.ToolTip = ConfigurationManager.AppSettings.GetValues("btnAgregar-ToolTip")[0];
                btnAgregar.CssClass = ConfigurationManager.AppSettings.GetValues("btnAgregar-CssClass")[0];

                // Buscar
                btnBuscar.ImageUrl = ConfigurationManager.AppSettings.GetValues("btnBuscar-ImageUrl")[0];
                btnBuscar.ToolTip = ConfigurationManager.AppSettings.GetValues("btnBuscar-ToolTip")[0];
                btnBuscar.AlternateText = ConfigurationManager.AppSettings.GetValues("btnBuscar-AlternateText")[0];
                btnBuscar.CssClass = ConfigurationManager.AppSettings.GetValues("btnBuscar-CssClass")[0];

                txtBuscar.Attributes.Add("placeholder", ConfigurationManager.AppSettings.GetValues("txtBuscar-placeholder")[0]);
                txtBuscar.CssClass = ConfigurationManager.AppSettings.GetValues("txtBuscar-CssClass")[0]; ;

                // Popup
                //-----------------
                lblAccion.Visible = false;
                lblAccion.Enabled = false;
                lblAccion.Text = string.Empty;

                pnlAgregarEditar.CssClass = "modal-dialog modal-lg";
                pnlAgregarEditar.Attributes.Add("Style", "display: none");


                // Popup Header
                btnPopX.Text = "X";
                btnPopX.CssClass = "close";
                //btnPopX.Attributes.Add("data-dismiss", "modal");


                lblPopupTitulo.CssClass = "modal-title";

                // Popup Contenido
                txtId.Enabled = false;

                lblNombre.Text = "Nombre";
                lblNombre.AssociatedControlID = "txtNombre";
                lblNombre.CssClass = "col-sm-2 control-label";

                lblAmPm.Text = "AM/PM";
                lblAmPm.AssociatedControlID = "ddlAmPm";
                lblAmPm.CssClass = "col-sm-2 control-label";

                lblDia.Text = "Dia";
                lblDia.AssociatedControlID = "ddlAmPm";
                lblDia.CssClass = "col-sm-2 control-label";

                lblHora.Text = "Hora";
                lblHora.AssociatedControlID = "ddlAmPm";
                lblHora.CssClass = "col-sm-2 control-label";

                lblMinutos.Text = "Minutos";
                lblMinutos.AssociatedControlID = "ddlAmPm";
                lblMinutos.CssClass = "col-sm-2 control-label";



                txtNombre.Attributes.Add("placeholder", "Ingresar Nombre");
                txtNombre.CssClass = "form-control";
                // Popup Footer
                btnPopCerrar.Text = "Cerrar";
                btnPopCerrar.CssClass = "btn btn-default";
                //btnPopCerrar.Attributes.Add("data-dismiss", "modal");

                btnPopGuardar.Text = "Guardar Cambios";

                btnPopGuardar.OnClientClick = "Guardar";

                this.Session["Grupo"] = "18";
                this.Page.Form.DefaultButton = btnBuscar.ID;
                this.Page.Form.DefaultFocus = txtBuscar.ID;
                RefrescarDatos();
            }
        }



        private DataTable Listar()
        {
            var oCn_Asambleas = new CN.Asambleas();
            if (txtBuscar.Text != string.Empty)
            {
                return oCn_Asambleas.ListarPorBus(txtBuscar.Text, Convert.ToInt64(this.Session["Grupo"]));
            }
            else
            {
                return oCn_Asambleas.Listar(Convert.ToInt64(this.Session["Grupo"]));
            }
        }


        private void RefrescarDatos()
        {
            lsvLista.DataSource = Listar();
            lsvLista.DataBind();
        }

        protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            (lsvLista.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            RefrescarDatos();
        }

        protected void Agregar(object sender, EventArgs e)
        {
            txtNombre.Text = string.Empty;
            txtNombre.Enabled = true;
            lblAccion.Text = "Agregar";
            lblPopupTitulo.Text = "Agregar Asamblea";
            btnPopGuardar.Text = "Agregar";
            btnPopGuardar.CssClass = "btn btn-primary";

            ddlDia.Enabled = true;
            txtNombre.Focus();
            popup.Show();
        }

        protected void Editar(object sender, EventArgs e)
        {
            ListViewDataItem item = (ListViewDataItem)(sender as Control).NamingContainer;
            Label lblID = (Label)item.FindControl("ID");
            Label lblNombre = (Label)item.FindControl("NOMBRE");
            Label lblDiaAsamblea = (Label)item.FindControl("DIA");
            Label lblHoraAsamblea = (Label)item.FindControl("HORA");
            txtId.Text = lblID.Text;
            txtNombre.Text = lblNombre.Text;
            txtNombre.Enabled = true;
            ddlDia.SelectedValue = lblDiaAsamblea.Text;
            ddlDia.Enabled = false;
            ddlHora.SelectedValue = lblHoraAsamblea.Text.Substring(0, 2);
            ddlMinutos.SelectedValue = lblHoraAsamblea.Text.Substring(3, 2);
            ddlAmPm.SelectedValue = lblHoraAsamblea.Text.Substring(6, 2) == "AM" ? "AM" : "PM";
            lblAccion.Text = "Editar";
            lblPopupTitulo.Text = "Editar Asamblea";
            btnPopGuardar.Text = "Guardar Cambios";
            btnPopGuardar.CssClass = "btn btn-primary";
            txtNombre.Focus();
            popup.Show();
        }
        protected void Eliminar(object sender, EventArgs e)
        {
            ListViewDataItem item = (ListViewDataItem)(sender as Control).NamingContainer;
            Label lblID = (Label)item.FindControl("ID");
            Label lblNombre = (Label)item.FindControl("NOMBRE");
            Label lblDiaAsamblea = (Label)item.FindControl("DIA");
            Label lblHoraAsamblea = (Label)item.FindControl("HORA");
            txtId.Text = lblID.Text;
            txtNombre.Text = lblNombre.Text;
            txtNombre.Enabled = false;
            ddlDia.SelectedValue = lblDiaAsamblea.Text;
            ddlHora.SelectedValue = lblHoraAsamblea.Text.Substring(0, 2);
            ddlMinutos.SelectedValue = lblHoraAsamblea.Text.Substring(3, 2);
            lblAccion.Text = "Eliminar";
            lblPopupTitulo.Text = "Eliminar Asamblea";
            btnPopGuardar.Text = "Eliminar";
            btnPopGuardar.CssClass = "btn btn-danger";
            popup.Show();
        }

        protected void Guardar(object sender, EventArgs e)
        {
            var oCn_Asambleas = new CN.Asambleas();
            var oCe_Asambleas = new CE.Asambleas();
            var strResultado = string.Empty;

            if (lblAccion.Text == "Editar")
            {
                oCe_Asambleas.NOMBRE = txtNombre.Text;
                oCe_Asambleas.ID_ASAMBLEA = Convert.ToInt64(txtId.Text);
                oCe_Asambleas.ID_GRUPO = Convert.ToInt64(this.Session["Grupo"]);
                oCe_Asambleas.DIA_ASAMBLEA = ddlDia.SelectedValue;


                if (Convert.ToInt16(ddlHora.SelectedValue) == 12)
                {
                    oCe_Asambleas.HORA_ASAMBLEA = (ddlAmPm.SelectedValue == "PM" ? ddlHora.SelectedValue : Convert.ToString(24)) + ddlMinutos.SelectedValue + "00";
                }
                else
                {
                    oCe_Asambleas.HORA_ASAMBLEA = (ddlAmPm.SelectedValue != "PM" ? ddlHora.SelectedValue : Convert.ToString(Convert.ToInt16(ddlHora.SelectedValue) + 12)) + ddlMinutos.SelectedValue + "00";
                }
                
                oCe_Asambleas.IsNew = false;
                strResultado = oCn_Asambleas.GuardarCambios(oCe_Asambleas, "Prueba");
                popup.Hide();
            }
            else if (lblAccion.Text == "Agregar")
            {
                oCe_Asambleas.NOMBRE = txtNombre.Text;
                oCe_Asambleas.ID_ASAMBLEA = 1;
                oCe_Asambleas.ID_GRUPO = Convert.ToInt64(this.Session["Grupo"]);
                oCe_Asambleas.DIA_ASAMBLEA = ddlDia.SelectedValue;
                oCe_Asambleas.HORA_ASAMBLEA = (ddlAmPm.SelectedValue != "PM" ? ddlHora.SelectedValue : Convert.ToString(Convert.ToInt16(ddlHora.SelectedValue) + 12)) + ddlMinutos.SelectedValue + "00";
                oCe_Asambleas.IsNew = true;
                strResultado = oCn_Asambleas.GuardarCambios(oCe_Asambleas, "Prueba");
                popup.Hide();
            }
            else if (lblAccion.Text == "Eliminar")
            {
                oCe_Asambleas.NOMBRE = txtNombre.Text;
                oCe_Asambleas.ID_ASAMBLEA = Convert.ToInt64(txtId.Text);
                oCe_Asambleas.ID_GRUPO = Convert.ToInt64(this.Session["Grupo"]);
                oCe_Asambleas.DIA_ASAMBLEA = ddlDia.SelectedValue;
                oCe_Asambleas.HORA_ASAMBLEA = (ddlAmPm.SelectedValue != "PM" ? ddlHora.SelectedValue : Convert.ToString(Convert.ToInt16(ddlHora.SelectedValue) + 12)) + ddlMinutos.SelectedValue + "00";
                oCe_Asambleas.IsNew = false;
                strResultado = oCn_Asambleas.Eliminar(oCe_Asambleas, "Prueba");
                DataPager pgr = lsvLista.FindControl("DataPager1") as DataPager;
                if (pgr != null && lsvLista.Items.Count != pgr.TotalRowCount)
                {
                    pgr.SetPageProperties(0, pgr.MaximumRows, false);
                }
            }
            // Validacion del resultado de la consulta.
            if (strResultado == "Cambios aplicados con exito.")
            {
                MostarExito(strResultado);
            }
            else if (strResultado == "El registro no se puede borrar por que tiene reistros relacionados.")
            {
                MostarInfo(strResultado);
            }
            else
            {
                MostarError("Ocurrio un error inesperado al guardar los cambios.");
            }
            RefrescarDatos();
            txtBuscar.Focus();
        }

        protected void Buscar(object sender, EventArgs e)
        {
            RefrescarDatos();

        }

        protected void CerrarPop(object sender, EventArgs e)
        {
            popup.Hide();
        }
    }
}